package com.gs.synedocket.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoNr;
import android.telephony.CellInfoWcdma;
import android.telephony.CellSignalStrengthCdma;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.CellSignalStrengthNr;
import android.telephony.CellSignalStrengthWcdma;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import java.util.List;


public class NetworkUtil {
    private static volatile NetworkUtil instance;
    private ConnectivityManager connectivity;

    public static NetworkUtil getInstance() {
        if (instance == null) {
            synchronized (NetworkUtil.class) {
                if (instance == null) {
                    instance = new NetworkUtil();
                }
            }
        }
        return instance;
    }

    public NetworkUtil() {
    }

    /**
     * 判斷設備是否有鏈接網絡
     *
     * @return
     */
    public boolean isConnected() {
        if (connectivity == null) {
            connectivity = (ConnectivityManager) AppUtil.getInstance().getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        }
        if (connectivity != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                NetworkCapabilities networkCapabilities = connectivity.getNetworkCapabilities(connectivity.getActiveNetwork());
                if (networkCapabilities != null) {
                    return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
                            || networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
                            || networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
                }
            } else {
                NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
                return networkInfo != null && networkInfo.isConnected();
            }
        }
        return false;
    }

    public boolean getNetworkSignal() {
        Context context = AppUtil.getInstance().getContext();
        if (connectivity == null) {
            connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        }
        if (connectivity != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                NetworkCapabilities networkCapabilities = connectivity.getNetworkCapabilities(connectivity.getActiveNetwork());
                if (networkCapabilities != null) {
                    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                        return getWifiSignal(context) > -100;
                    }
                    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                        return getMobileSignal(context) > -120;
                    }
                }
            } else {
                NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {
                    NetworkInfo wifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    if (wifi != null && wifi.isConnected()) {
                        return getWifiSignal(context) > -100;
                    }
                    NetworkInfo mobile = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    if (mobile != null && mobile.isConnected()) {
                        return getMobileSignal(context) > -120;
                    }
                }
            }
        }
        return false;
    }

    public int getWifiSignal(Context context) {
        WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wm.getConnectionInfo();
        int rssi = wifiInfo.getRssi();
        Log.i("wifi",rssi+"");
        return rssi;
    }

    public int getMobileSignal(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        int dbm = 0;
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return dbm;
        }
        List<CellInfo> cellInfoList = tm.getAllCellInfo();
        if (cellInfoList != null && !cellInfoList.isEmpty()) {
            for (CellInfo cellInfo : cellInfoList) {
                if (!cellInfo.isRegistered()) {
                    continue;
                }
                if (cellInfo instanceof CellInfoGsm) {
                    CellSignalStrengthGsm gsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
                    dbm = gsm.getDbm();
                } else if (cellInfo instanceof CellInfoCdma) {
                    CellSignalStrengthCdma cdma = ((CellInfoCdma) cellInfo).getCellSignalStrength();
                    dbm = cdma.getDbm();
                } else if (cellInfo instanceof CellInfoLte) {
                    CellSignalStrengthLte cdma = ((CellInfoLte) cellInfo).getCellSignalStrength();
                    dbm = cdma.getDbm();
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    if (cellInfo instanceof CellInfoNr) {
                        CellSignalStrengthNr nr = (CellSignalStrengthNr) ((CellInfoNr) cellInfo).getCellSignalStrength();
                        dbm = nr.getSsRsrp();
                        if (dbm > 0) {
                            dbm = -dbm;
                        }
                    }
                } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    if (cellInfo instanceof CellInfoWcdma) {
                        CellSignalStrengthWcdma cdma = ((CellInfoWcdma) cellInfo).getCellSignalStrength();
                        dbm = cdma.getDbm();
                    }
                }
            }
        }
        return dbm;
    }
}
